
enum BAUD_RATE {
    //% block="9600"
    9600,
    //% block="115200"
    115200
}
enum dataBlock {
    //% block="1"
1,
    //% block="2"
     2,
    //% block="3"
3,
    //% block="4"
 4,
    //% block="5"
5,
    //% block="6"
6,
    //% block="7"
7,
    //% block="8"
8,
    //% block="9"
9,
    //% block="10"
10,
    //% block="11"
11,
    //% block="12"
12,
    //% block="13"
13,
    //% block="14"
14,
    //% block="15"
    15,
    //% block="16"
16,
    //% block="17"
17,
    //% block="18"
 18,
    //% block="19"
19,
    //% block="20"
20,
    //% block="21"
21,
    //% block="22"
22,
    //% block="23"
23,
    //% block="24"
 24,
    //% block="25"
 25,
    //% block="26"
26,
    //% block="27"
 27,
    //% block="28"
28,
    //% block="29"
29,
    //% block="30"
30,
    //% block="31"
31,
    //% block="32"
32,
    //% block="33"
 33,
    //% block="34"
 34,
    //% block="35"
35,
    //% block="36"
36,
    //% block="37"
37,
    //% block="38"
38,
    //% block="39"
39,
    //% block="40"
40,
    //% block="41"
41,
    //% block="42"
 42,
    //% block="43"
 43,
    //% block="44"
 44,
    //% block="45"
 45,
    //% block="46"
 46,
    //% block="47"
47,
    //% block="48"
 48,
    //% block="49"
49,
    //% block="50"
50,
    //% block="51"
51,
    //% block="52"
 52,
    //% block="53"
53,
    //% block="54"
 54,
    //% block="55"
55,
    //% block="56"
 56,
    //% block="57"
 57,
    //% block="58"
  58,
    //% block="59"
59,
    //% block="60"
 60,
    //% block="61"
61,
    //% block="62"
62,
}

enum ByteID {
    //% block="1"
1,
    //% block="2"
     2,
    //% block="3"
3,
    //% block="4"
 4,
    //% block="5"
5,
    //% block="6"
6,
    //% block="7"
7,
    //% block="8"
8,
    //% block="9"
9,
    //% block="10"
10,
    //% block="11"
11,
    //% block="12"
12,
    //% block="13"
13,
    //% block="14"
14,
    //% block="15"
    15,
    //% block="16"
16
}
enum ASRDATA {
    //% block="开灯"
    1,
     //% block="关灯"
    2,
     //% block="亮一点"
    3,
     //% block="暗一点"
    4,
     //% block="打开窗帘"
    17,
     //% block="关闭窗帘"
    18,
     //% block="打开空调"
    33,
     //% block="关闭空调"
    34,
     //% block="调高温度"
    35,
     //% block="调低温度"
    36,
     //% block="睡眠模式"
    37,
     //% block="二十六度"
    38,
    //% block="打开风扇"
    49,
	//% block="关闭风扇"
    50,
	//% block="风速一档"
    51,
	//% block="风速二档"
    52,
	//% block="风速三档"
    53,
	//% block="播放音乐"
    65,
	//% block="暂停播放"
    66,
	//% block="停止播放"
    67,
	//% block="上一曲"
    68,
	//% block="下一曲"
    69,
	//% block="响一点"
    70,	
	//% block="轻一点"
    71,
	//% block="前进"
    81,
	//% block="后退"
    82,
	//% block="左转"
    83,
	//% block="右转"
    84,	
	//% block="停止"
    85,
	//% block="加速"
    86,
	//% block="减速"
    87,
	//% block="正转"
    88,	
	//% block="反转"
    89,	
	//% block="开机"
    90,	
	//% block="关机"
    91,	
}
enum DHT_TH {
    //% blockId="DHT_T" block="temperature(℃)"
    Temperature,
    //% blockId="DHT_H" block="humidity(%rh)"
    Humidity
}
enum TTS_VAR {
//% block="0"
    0,
    //%block="1"
    1,
    //%block="2"
    2,
    //% block="3"
    3,
    //%block="4"
    4,
    //% block="5"
    5,
    //%block="6"
    6,
    //% block="7"
    7,
    //%block="8"
    8,
    //% block="9"
    9
}

enum TTS_RING {
    //% block="ring_1"
    ring_1,
    //%block="ring_2"
    ring_2,
    //%block="ring_3"
    ring_3,
    //% block="ring_4"
    ring_4,
    //%block="ring_5"
    ring_5,
    //% block="message_1"
    message_1,
    //%block="message_2"
    message_2,
    //% block="message_3"
    message_3,
    //%block="message_4"
    message_4,
    //% block="message_5"
    message_5,
    //%block="alert_1"
    alert_1,
    //% block="alert_2"
    alert_2,
    //%block="alert_3"
    alert_3,
    //% block="alert_4"
    alert_4,
    //%block="alert_5"
    alert_5
}

enum ONOFF {
    //% block="HIGH"
    HIGH,
        //% block="LOW"
    LOW,
}
enum UNIT {
    //% blockId="UNIT_CM" block="cm"
    CM,
    //% blockId="UNIT_IN" block="inch"
    IN,
}
//% color="#30C08D" iconWidth=50 iconHeight=40
namespace Maker_mpython_box {

  //% block="set  usb power on [USBPIN] output [STATE]" blockType="command"
    //% USBPIN.shadow="dropdown" USBPIN.options="USBPIN"
    //% STATE.shadow="dropdown" STATE.options="ONOFF" STATE.defl="HIGH"
    export function setUSBModuleState(parameter: any, block: any) {
        let outputModulePin = parameter.USBPIN.code;
        let outputModuleState = parameter.STATE.code;
        Generator.addCode(`digitalWrite(${outputModulePin},${outputModuleState});`);
    }

  //% block="set  servo  [SERVOPIN] output [DEGREE]" blockType="command"
    //% SERVOPIN.shadow="dropdown" SERVOPIN.options="SERVOPIN"
    //%DEGREE.shadow="angle" DEGREE.params.edge=1
    export function setServoModuleDegree(parameter: any, block: any) {
        let SERVOPIN = parameter.SERVOPIN.code;
        let DEGREE = parameter.DEGREE.code;
        Generator.addInclude("include_DFRobot_Servo", `#include <DFRobot_Servo.h>`);
        Generator.addObject("object_Servo", `Servo `, `Servo_${SERVOPIN};`);
        Generator.addSetup(`initSetup_Servo`, `Servo_${SERVOPIN}.attach(${SERVOPIN});`);
        Generator.addCode(`Servo_${SERVOPIN}.angle(abs(${DEGREE}));`);
    }


  //% block="set  relay on [RELAYPIN] output [STATE]" blockType="command"
    //% RELAYPIN.shadow="dropdown" RELAYPIN.options="RELAYPIN"
    //% STATE.shadow="dropdown" STATE.options="ONOFF" STATE.defl="HIGH"
    export function setRelayModuleState(parameter: any, block: any) {
        let outputModulePin = parameter.RELAYPIN.code;
        let outputModuleState = parameter.STATE.code;
        Generator.addCode(`digitalWrite(${outputModulePin},${outputModuleState});`);
    }

    //% block="set usb power on [USBPIN] output [USBVALUE]" blockType="command"
    //% USBPIN.shadow="dropdown" USBPIN.options="USBPIN"
    //% USBVALUE.shadow="range"   USBVALUE.params.min=0    USBVALUE.params.max=1023    USBVALUE.defl=200  
    export function setUSBModuleValue(parameter: any, block: any) {
        let outputModulePin = parameter.USBPIN.code;
        let outputModuleState = parameter.USBVALUE.code;
            Generator.addCode(`analogWrite(${outputModulePin},${outputModuleState});`);
    }


    //% block="read water on [IAMPIN]" blockType="reporter"
    //% IAMPIN.shadow="dropdown" IAMPIN.options="WaterPIN"
    export function readWaterModule(parameter: any, block: any) {
        let inputModulePin = parameter.IAMPIN.code;
        Generator.addCode(`analogRead(${inputModulePin})`);
    }

    //% block="read ds18b20 on [extPIN]" blockType="reporter"
    //% extPIN.shadow="dropdown" extPIN.options="extPIN"
    export function readDS18B20Module(parameter: any, block: any) {
        let inputModulePin = parameter.extPIN.code;
    Generator.addInclude("include_DFRobot_DS18B20", `#include <DFRobot_DS18B20.h>`);
        Generator.addObject("object_DFRobot_DS18B20", `DFRobot_DS18B20`, `ds18b20_${inputModulePin};`);
        Generator.addSetup(`initSetup_DFRobot_DS18B20`, `ds18b20_${inputModulePin}.begin(${inputModulePin});`);
        Generator.addCode(`ds18b20_${inputModulePin}.getTempC()`);
    }

    //% block="read T&H on [TH]" blockType="reporter"
    //% TH.shadow="dropdown" TH.options="DHT_TH" TH.defl="DHT_TH.Temperature"
    export function readAhtSensor(parameter: any, block: any) {
        let aht_th = parameter.TH.code;
        Generator.addInclude("include_Wire", `#include <Wire.h>`);
        Generator.addInclude("include_AHTxx", `#include <AHTxx.h>`);
        Generator.addObject("object_AHTxx", `AHTxx `, `aht20(AHTXX_ADDRESS_X38, AHT2x_SENSOR);`);
        Generator.addSetup(`initSetup_AHTxx`, `aht20.begin();`);
        Generator.addCode(`aht20.read${aht_th}()`);
    }

    //% block="read ulrasonic sensor Unit [UNIT] trig引脚[TRIGPIN] echo引脚 [ECHOPIN]" blockType="reporter"
    //% UNIT.shadow="dropdown" UNIT.options="UNIT" UNIT.defl="UNIT.CM"
    //% TRIGPIN.shadow="dropdown" TRIGPIN.options="TRIGPIN"
    //% ECHOPIN.shadow="dropdown" ECHOPIN.options="ECHOPIN"
    // export function readUlrasonicSensor(parameter: any, block: any) {
    //     let unit = parameter.UNIT.code;
    //     let trigpin = parameter.TRIGPIN.code;
    //     let echopin = parameter.ECHOPIN.code;
    //     Generator.addInclude("include_DFRobot_URM10", `#include <DFRobot_URM10.h>`);
    //     Generator.addObject("object_DFRobot_URM10", `DFRobot_URM10`, `sr04;`);
    //     Generator.addCode(`sr04.getDistance${unit}(${trigpin},${echopin})`);
    // }

    //% block="read ulrasonic sensor 引脚[CSBPIN]" blockType="reporter"
    //% CSBPIN.shadow="dropdown" CSBPIN.options="CSBPIN"
    export function readUlrasonic2Sensor(parameter: any, block: any) {
        let CSBPIN = parameter.CSBPIN.code;
        Generator.addInclude("include_Ultrasonic", `#include <Ultrasonic.h>`);
        Generator.addObject("object_Ultrasonic", `Ultrasonic `, `ultrasonic(${CSBPIN});`);
        Generator.addCode(`ultrasonic.MeasureInCentimeters()`);
    }

    //% block="init ASR module [RXPIN]" blockType="command"
    //% RXPIN.shadow="dropdown" RXPIN.options="RXPIN"
    export function asrSetup(parameter: any, block: any) {
        let RXPIN = parameter.RXPIN.code
        Generator.addSetup("Serial1Setup",`Serial1.begin(9600 ,P9,-1);`);
    }

    //% block="有语音可读?" blockType="boolean"
    export function available() {
        Generator.addCode(`Serial1.available()`)
    }


    //% block="read asr result" blockType="command"
    export function readAsr(parameter: any, block: any) {
        Generator.addCode(`int asrdata = Serial1.read();`)
    }


    //% block="读取语音为[ASR]" blockType="boolean"
    //% ASR.shadow="dropdown" ASR.options="ASRDATA" ASR.defl="ASR.1"
    export function asrdata(parameter: any, block: any) {
        let str = parameter.ASR.code
        Generator.addCode(`asrdata==${str}`);
    }

    //% block="read ASR Number" blockType="reporter"
    export function readAasrNum() {
        Generator.addCode(`asrdata`)
    }
    
    //% block="init tts module[TXPIN]" blockType="command"
    //% TXPIN.shadow="dropdown" TXPIN.options="TXPIN"
    export function ttsSetup(parameter: any, block: any) {
        let TXPIN = parameter.TXPIN.code
        Generator.addInclude('ruilong_ai_box', '#include <ruilong_ai_box.h>');
         Generator.addSetup("Serial2Setup",`Serial2.begin(115200 ,-1,P8);`);
    }

    //% block="语音合成 语调[tts_tone] 语速[tts_speed]音量[tts_volume][VAL]" blockType="command"
    //% tts_tone.shadow="dropdown" tts_tone.options="TTS_VAR" tts_tone.defl="TTS_VAR.5"
    //% tts_speed.shadow="dropdown" tts_speed.options="TTS_VAR" tts_speed.defl="TTS_VAR.5"
    //% tts_volume.shadow="dropdown" tts_volume.options="TTS_VAR" tts_volume.defl="TTS_VAR.5"
    //% VAL.shadow="string" VAL.defl=你好
    export function tts(parameter: any, block: any) {
        let tts_tone = parameter.tts_tone.code;
        let tts_speed = parameter.tts_speed.code;
        let tts_volume = parameter.tts_volume.code;
        let str = parameter.VAL.code;
        //str=str.substring(1);
        let new_str="\"[t"+tts_tone.toString()+"][s"+tts_speed.toString()+"][v"+tts_volume.toString()+"]\"+String("+str+")";
        Generator.addCode(`TTS(${new_str});`);
    }

    //% block="铃声播报 音量[tts_volume] 铃声[tts_ring]" blockType="command"
    //% tts_volume.shadow="dropdown" tts_volume.options="TTS_VAR" tts_volume.defl="TTS_VAR.5"
    //% tts_ring.shadow="dropdown" tts_ring.options="TTS_RING" tts_ring.defl="TTS_RING.ring_1"
    export function ttsring(parameter: any, block: any) {
        let tts_volume = parameter.tts_volume.code;
        let tts_ring = parameter.tts_ring.code;
        let new_str="\"[v"+tts_volume.toString()+"]，"+tts_ring+"\"";
        Generator.addInclude('ruilong_ai_box', '#include <ruilong_ai_box.h>');
        Generator.addCode(`TTS(${new_str});`);
    }

    //% block="read Serial str" blockType="reporter"
    export function readSerialstr() {
        Generator.addCode(`Serial.readStringUntil('@')`)
    }
    
    //% block="初始化RFID模块 接口I2C 地址0x28" blockType="command"
    //% TXPIN.shadow="dropdown" TXPIN.options="TXPIN"
    export function RFID_Setup(parameter: any, block: any) {
       // let TXPIN = parameter.TXPIN.code
 Generator.addInclude('ruilong_Wire', '#include <Wire.h>');
        Generator.addInclude('ruilong_MFRC522_I2C', '#include <RL_MFRC522_I2C.h>');
        Generator.addObject("object_MFRC522", `MFRC522 `, ` rfid(0x28,-1);`);
        Generator.addSetup(`initWire_begin`, `Wire.begin();`);
        Generator.addSetup(`initrfid`, `rfid.PCD_Init();`);
    }

    //% block="RFID检测到卡片？" blockType="boolean"
    export function RFID_available() {
        Generator.addCode(`rfid.IsNewCard()`)
    }
    //% block="读取RFID模块UID" blockType="reporter"
    //% ASR.shadow="dropdown" ASR.options="ASRDATA" ASR.defl="ASR.1"
    export function RFIDdata(parameter: any, block: any) {
    Generator.addCode(`rfid.ReadCardUID()`);
    }
    
    //% block="读取RFID模块 数据块[dataBlock]第[ByteID]字节" blockType="reporter"
    //% dataBlock.shadow="dropdown" dataBlock.options="dataBlock" dataBlock.defl="1"
    //% ByteID.shadow="dropdown" ByteID.options="ByteID" ByteID.defl="1"
    export function RFIDReadCardbyte(parameter: any, block: any) {
        let dataBlock = parameter.dataBlock.code;
        let ByteID = parameter.ByteID.code;
    Generator.addCode(`rfid.ReadCardbyte( ${dataBlock}, ${ByteID})`);
    }


    //% block="RFID模块 数据块[dataBlock]第[ByteID]字节写入[data]" blockType="command"
    //% dataBlock.shadow="dropdown" dataBlock.options="dataBlock" dataBlock.defl="1"
    //% ByteID.shadow="dropdown" ByteID.options="ByteID" ByteID.defl="1"
    //% data.shadow="range"   data.params.min=0    data.params.max=255    data.defl=100  
    export function RFIDWriteCardbyte(parameter: any, block: any) {
        let dataBlock = parameter.dataBlock.code;
        let ByteID = parameter.ByteID.code;
        let data = parameter.data.code;
        Generator.addCode(`rfid.WriteCardbyte( ${dataBlock}, ${ByteID},${data});`);
    }

    //% block="RFID模块 终止卡片通讯" blockType="command"
      export function RFIDStopComm(parameter: any, block: any) {
        Generator.addCode(`rfid.PICC_HaltA();\nrfid.PCD_StopCrypto1();`);
    }
}


